﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace PeisongWeb.admin
{
    public partial class bueryRangeEdit2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rootCategoryBind();
                subCategoryBind();
                rptBind();

                Panel1.Title = string.Format("#{0}#采购范围", logic.company.getById(companyId).Company);
            }
        }

        public int companyId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["company"]))
                {
                    return Convert.ToInt32(Request.QueryString["company"]);
                }
                return 0;
            }
            set
            {
                companyId = value;
            }
        }

        private void rootCategoryBind()
        {
            DataTable dt = logic.buyerRange.rootCategorylistWithCompanyId(companyId);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        private void subCategoryBind()
        {
            //当大类不等于空的时候，就绑定小类
            if (ddl_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.buyerRange.subCategorylistWithCompanyIdAndParentCategoryId(companyId, Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
            }
            else
            {
                ddl_CategorySub.Items.Clear();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            where.AppendFormat(" and buyerId={0}",companyId);
            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}",ddl_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txt_ProductName.Text))
                where.AppendFormat(" and [name] like '%{0}%'",txt_ProductName.Text);
            if (!string.IsNullOrEmpty(txt_ProductNo.Text))
                where.AppendFormat(" and productNo='{0}'", txt_ProductNo.Text);

            DataSet ds = logic.buyerRange.list(where.ToString(), "productId,businessNature", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender,EventArgs e)
        {
            rptBind();
        }

        // 搜索
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbn_reset_Click(object sender,EventArgs e)
        {
        }

        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender,EventArgs e)
        {
            subCategoryBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="Del")
            {
                int productId = 0;
                string businessnature;
                string[] productidAndbusinessnature = e.CommandArgument.ToString().Split(',');
                productId = Convert.ToInt32(productidAndbusinessnature[0]);
                businessnature = productidAndbusinessnature[1];
                logic.buyerRange.delete(companyId,productId,businessnature);

                Library.Script.ClientMsg("删除成功.");

                rptBind();
            }
            if (e.CommandName == "ReamrksEdit")
            {
                string[] stritems = e.CommandArgument.ToString().Split(',');
                int productId = Convert.ToInt32(stritems[0]);
                string businessnature = stritems[1];
                TextBox txtamount = (TextBox)e.Item.FindControl("txt_amount");
                double amount = Convert.ToDouble(txtamount.Text.ToString());
                TextBox txtremarks = (TextBox)e.Item.FindControl("txt_remarks");
                string remarks = txtremarks.Text.ToString();
                logic.buyerRange.buyerRangeUpdateByRemark(companyId,productId, remarks,amount,businessnature);
                rptBind();
            }
        }

        // 删除选择项
        protected void btnDelSelected_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hdSelectedProductIds.Value))
            {
                string[] allitems = hdSelectedProductIds.Value.Split(',');
                string[] productId = new string[allitems.Length];
                string[] businessnature = new string[allitems.Length];
                for (int i = 0; i < allitems.Length; i++)
                {
                    string[] pidandbnature=allitems[i].Split('|');
                    productId[i]=pidandbnature[0];
                    businessnature[i]=pidandbnature[1];
                }
                for (int i = 0; i < allitems.Length; i++)
                {
                    logic.buyerRange.delete(companyId,Convert.ToInt32( productId[i]), businessnature[i]);
                }
                    rptBind();

                Library.Script.ClientMsg("删除成功.");
            }
        }

        // 按大类删除
        protected void btnRootCategoryDel_Click(object sender, EventArgs e)
        {
            int rootCategory = 0;
            int.TryParse(ddl_CategoryRoot.SelectedValue,out rootCategory);

            if (rootCategory!=0)
            {
                logic.buyerRange.deleteByRootCategory(rootCategory,companyId);
                rptBind();

                Library.Script.ClientMsg("删除成功.");
            }
        }

        // 按小类删除
        protected void btnSubCategoryDel_Click(object sender, EventArgs e)
        {
            int subCategory = 0;
            int.TryParse(ddl_CategorySub.SelectedValue, out subCategory);

            if (subCategory != 0)
            {
                logic.buyerRange.deleteBySubCategory(subCategory, companyId);
                rptBind();

                Library.Script.ClientMsg("删除成功.");
            }
        }

        // 新增供应范围或修改
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("buyerRangeEdit1.aspx?company={0}", companyId));
        }

        protected void btn_save_ExtendDay_Click(object sender, EventArgs e)
        {
            //logic.buyerRange.buyerRangeUpdateByRemark(companyId, Convert.ToInt32(HiddenProductId.Value), txt_Remarks.Text.ToString());
            //rptBind();
        }
        //public string BusinessNature(string businessnature)
        //{
        //    if (businessnature == "1")
        //        return "工厂食堂级";
        //    else if (businessnature == "2")
        //        return "机关事业单位级";
        //    else if (businessnature == "3")
        //        return "院校级";
        //    else
        //        return "酒店餐饮级";

        //}
    }
}
